package com.neusoft.neuiotms.dcom.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.ShiftDTO;
import com.neusoft.neuiotms.dcom.model.DutyArrangement;
import com.neusoft.neuiotms.dcom.model.Shift;
import com.neusoft.neuiotms.dcom.repository.DutyArrangementRepository;
import com.neusoft.neuiotms.dcom.repository.ShiftRepository;

@Component
public class ShiftConverter extends AbstractConverter<Shift, ShiftDTO> {

    @Autowired
    private ShiftRepository shiftRepository;
    @Autowired
    private DutyArrangementRepository dutyArrangementRepository;

    @Override
    public Shift toModel(final ShiftDTO dto) {

        Shift model;
        if (dto.isNew()) {
            model = new Shift();
        } else {
            model = this.shiftRepository.getOne(dto.getId());
        }

        model.setSourceUserCode(dto.getSourceUserCode());
        model.setSourceUserName(dto.getSourceUserName());
        model.setStartTime(dto.getStartTime());
        model.setEndTime(dto.getEndTime());
        model.setTargetUserCode(dto.getTargetUserCode());
        model.setTargetUserName(dto.getTargetUserName());
        model.setShiftStartTime(dto.getShiftStartTime());
        model.setShiftEndTime(dto.getShiftEndTime());
        model.setType(dto.getType());
        model.setStatus(dto.getStatus());

        if (null != dto.getDutyArrangementId()) {
            final DutyArrangement dutyArrangement = this.dutyArrangementRepository.getOne(dto.getDutyArrangementId());
            model.setDutyArrangement(dutyArrangement);
        }

        return model;
    }

    @Override
    public ShiftDTO toDTO(final Shift model, final boolean forListView) {

        final ShiftDTO dto = new ShiftDTO();

        dto.setId(model.getId());
        dto.setSourceUserCode(model.getSourceUserCode());
        dto.setSourceUserName(model.getSourceUserName());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());
        dto.setTargetUserCode(model.getTargetUserCode());
        dto.setTargetUserName(model.getTargetUserName());
        dto.setShiftStartTime(model.getShiftStartTime());
        dto.setShiftEndTime(model.getShiftEndTime());
        dto.setType(model.getType());
        dto.setStatus(model.getStatus());

        if (null != model.getDutyArrangement()) {
            dto.setOrgCode(model.getDutyArrangement().getOrgCode());
            dto.setOrgName(model.getDutyArrangement().getOrgName());
            dto.setDutyArrangementId(model.getDutyArrangement().getId());
        }

        return dto;
    }

}
